package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.RoleDto;
import com.itheima.ydd.entity.RoleMenu;
import com.itheima.ydd.service.RoleMenuService;
import com.itheima.ydd.mapper.RoleMenuMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author xyiaao
 * @description 针对表【sys_role_menu(角色-菜单)】的数据库操作Service实现
 * @createDate 2022-02-24 21:23:31
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu>
        implements RoleMenuService {

    @Override
    public void updateRoleMenu(RoleDto roleDto) {

        // 先根据roleId删除
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId, roleDto.getId());
        super.remove(queryWrapper);
        // 再依次添加
        // 按条存储
        // for (Integer menuId : roleDto.getMenuIds()) {
        //     RoleMenu roleMenu = new RoleMenu();
        //     roleMenu.setRoleId(roleDto.getId());
        //     roleMenu.setMenuId(menuId.toString());
        //     super.save(roleMenu);
        // }


        // 按集合存储
        List<RoleMenu> roleMenus = new LinkedList<>();
        // List<RoleMenu> roleMenus = new ArrayList<>(roleDto.getMenuIds().size());

        for (Integer menuId : roleDto.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleDto.getId());
            roleMenu.setMenuId(menuId.toString());
            roleMenus.add(roleMenu);
        }
        super.saveBatch(roleMenus);
    }

}




